package com.blockmeta.bbs.overallserviceapplication.search.adapter;


import android.content.Context;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.base.adapter.BaseAdapter;
import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.baselibrary.utils.richtext.RichText;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.overallserviceapplication.R;
import com.blockmeta.bbs.overallserviceapplication.pojo.WeiBoPOJO;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by author on 2018/1/9.
 * 需要加放大缩小图片的逻辑
 */

public class WeiBoListAdapter extends BaseAdapter<WeiBoPOJO, BaseViewHolder> {
    private Context context;
    private List<String> listImage;
    private RecyclerView weibo_recy;
    private GridLayoutManager manager;
    private GridLayoutManager manager1;
    private SpannableStringBuilder mBuilder;

    public WeiBoListAdapter(int layoutResId, @Nullable List<WeiBoPOJO> data, Context context) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, final WeiBoPOJO item) {
        listImage = item.getImg();
        Glide.with(mContext).load(item.getLogo()).into((ImageView) helper.getView(R.id.weibo_img));
        helper.setText(R.id.weibo_title, item.getNickname());
        helper.setText(R.id.weibo_time, FormatUtil.getTimetoHMS(item.getWb_time()));
        TextView webview = helper.getView(R.id.weibo_webview);
        String regex1 = "<span class='url-icon'[^<>]*>(.*?)<\\/span>";
        RichText.from(item.getContent().replaceAll(regex1,"")).into(webview);
        TextView webview2 = helper.getView(R.id.weibo_webview2);
        LinearLayout wei_ll = helper.getView(R.id.weibo_ll);

        if (!TextUtils.isEmpty(item.getFrom_user())) {
            wei_ll.setVisibility(View.VISIBLE);
            String fromstyle = "<a href=' '>@" + item.getFrom_user() + "</a >";
            RichText.from(fromstyle + ": " + item.getRepost_content().replaceAll(regex1,"")).into(webview2);
        } else {
            wei_ll.setVisibility(View.GONE);
        }
        webview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RouterHelper.startActivity(mContext, RouterPath.WEB_ACTIVITY,item.getScheme(),"微博");
            }
        });
        wei_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        webview2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RouterHelper.startActivity(mContext, RouterPath.WEB_ACTIVITY,item.getScheme(),"微博");
            }
        });


        //图片设置
        loadWithImage(helper, item);
    }

    /**
     * 加载图片的处理
     *
     * @param helper
     * @param item
     */
    private void loadWithImage(BaseViewHolder helper, final WeiBoPOJO item) {
        weibo_recy = (RecyclerView) helper.getView(R.id.weibo_recy);
        if (item.getImg().size() % 3 == 0) {
            manager = new GridLayoutManager(context, 3);
        } else if (item.getImg().size() <= 4) {
            manager = new GridLayoutManager(context, 2);
        } else {
            manager = new GridLayoutManager(context, 3);
        }
        weibo_recy.setLayoutManager(manager);
        weibo_recy.setHasFixedSize(true);
        WeiBoImageAdapter weiBoImageAdapter = new WeiBoImageAdapter(R.layout.item_grid_image, listImage);
        weibo_recy.setAdapter(weiBoImageAdapter);
        weiBoImageAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            private String replace;

            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ArrayList<String> pics = new ArrayList<>();
                for (int i = 0; i < item.getImg().size(); i++) {
                    String everyPhoto = item.getImg().get(i);
                    try {
                        String mReplaceFlag = item.getReplace_pic_flag();
                        String[] split = mReplaceFlag.split(",");
                        replace = everyPhoto.replace(split[0], split[1]);
                    } catch (Exception e) {
                        replace = everyPhoto.replace("bmiddle", "large");
                        replace = replace.replace("orj360", "mw690");
                    }

                    pics.add(replace);
                }
                RouterHelper.buildPath(RouterPath.VIEWPAGER_ACTIVITY)
                        .withStringArrayList(Constant.ZERO,new ArrayList<>(pics))
                        .withInt(Constant.ONE,position)
                        .navigation(context);
            }
        });

    }

    public class WeiBoImageAdapter extends BaseAdapter<String, BaseViewHolder> {

        public WeiBoImageAdapter(int layoutResId, @Nullable List<String> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, final String item) {
            Glide.with(context).load(item).into((ImageView) helper.getView(R.id.weibo_each_img));
        }

    }


}
